package artid.com.arttogo.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import artid.com.arttogo.R;
import artid.com.arttogo.bean.SearchRequestBeanSend;
import artid.com.arttogo.db.Cache;
import artid.com.arttogo.network.VolleyBasic;
import artid.com.arttogo.network.VolleyCallback;
import artid.com.arttogo.network.VolleyData;
import artid.com.arttogo.network.VolleyMap;
import artid.com.arttogo.utils.VeDate;
import artid.com.arttogo.view.horizonlistview.NewartistArtistListParser;
import artid.com.arttogo.view.horizonlistview.NewartistArtistList_Adapter;
import artid.com.arttogo.view.pulllist.PullToRefreshLayout;
import artid.com.arttogo.view.pulllist.PullableListView;

/**
 * 
 * 
 * 项目名称：ArtworkArt 类名称：SearchResults_Activity 类描述：搜索结果页面 创建人：耿卫斌 创建时间：2017-1-6
 * 下午3:03:48
 *
 */
public class SearchResults_Activity extends BaseActivity implements
		OnClickListener {
	private Context mContext;
	private PullableListView mListView;
	private PullToRefreshLayout mPullLayout;
	private boolean isRefreshed = false;// 是否刷新
	private int page = 1;
	private List<NewartistArtistListParser.NewartistArtistList_ArtistList> list;
	private List<NewartistArtistListParser.NewartistArtistList_ArtistList> more_list;
	private NewartistArtistList_Adapter adapter;
	private List<SearchRequestBeanSend> list_search;// 搜索数据
	private String data;// 搜索Json串
	private String keyword = "";// 搜索关键词
	private LinearLayout mLineMain;//搜索条件View
	private LinearLayout noDataLL;//无数据布局
	View mViewHint;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_searchresults);
		mContext = SearchResults_Activity.this;
		
		initView();
	}

	@SuppressLint("NewApi")
	private void initView() {
		list = new ArrayList<NewartistArtistListParser.NewartistArtistList_ArtistList>();
		more_list = new ArrayList<NewartistArtistListParser.NewartistArtistList_ArtistList>();
		mLineMain = (LinearLayout) findViewById(R.id.srh_result_tagmain);
		mBack = (ImageView) findViewById(R.id.back_btn);
		mBack.setVisibility(View.VISIBLE);
		mBack.setOnClickListener(this);
		mViewHint = (View)findViewById(R.id.src_hint);
		mTittle = (TextView) findViewById(R.id.tittle_text);
		mTittle.setText("搜索结果");
		noDataLL = (LinearLayout) findViewById(R.id.ll_hint);
		mListView = (PullableListView) findViewById(R.id.SearchResults_ListView);
		mPullLayout = (PullToRefreshLayout) findViewById(R.id.SearchResults_refresh_view);
		mPullLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {

			@Override
			public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
				// TODO Auto-generated method stub
				// 下拉刷新
				page = 1;
				isRefreshed = true;
				searchRequestData();
			}

			@Override
			public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
				// TODO Auto-generated method stub
				// pullToRefreshLayout.setBottomDismiss();
				// 上拉加载
				page++;
				searchRequestData();
			}
		});
		searchRequestData();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back_btn:
			// 返回
			finish();
			break;

		default:
			break;
		}
	}

	/**
	 * 搜索数据
	 */
	@SuppressWarnings("unchecked")
	private void searchRequestData() {
		list_search = new ArrayList<SearchRequestBeanSend>();
		list_search=(List<SearchRequestBeanSend>) getIntent().getSerializableExtra("list_search");
		keyword = getIntent().getExtras().getString("keyword");
		setTags(list_search);
		Gson gson = new Gson();
		data = gson.toJson(list_search);
		getArtistListData();
	}

	/**
	 * 获取艺术家信息和作品信息。 页数 page 条数 pagesize 选择条件 data 搜索条件 keyword
	 */
	private void getArtistListData() {
		VolleyBasic.getInstance(this).post(
				Cache.searchartistList,
				VolleyMap.getInstance()
				.searchartistList(page, 10, data, keyword),
				NewartistArtistListParser.class, new VolleyCallback() {

					@Override
					public void onSuccess(VolleyData vd) {
						// TODO Auto-generated method stub
						mPullLayout.loadmoreFinish(mPullLayout.SUCCEED);
						NewartistArtistListParser mBean = new NewartistArtistListParser();
						mBean = (NewartistArtistListParser) vd.getData();
						list = mBean.getArtistList();
						if (isRefreshed == true) {
							more_list.clear();
							isRefreshed = false;
						}
						if (!list.isEmpty()) {
							more_list.addAll(list);
						}
						// 这里是显示有数据和无数据的布局
						if (more_list != null && more_list.size() != 0) {
							mViewHint.setVisibility(View.GONE);
						} else {
							mViewHint.setVisibility(View.VISIBLE);
							return;
						}
						if (adapter == null) {
							adapter = new NewartistArtistList_Adapter(mContext,
									more_list, true);
							mListView.setAdapter(adapter);
						}
						adapter.notifyDataSetChanged();
					}

					@Override
					public void onError(VolleyError volleyError) {
						// TODO Auto-generated method stub
						mPullLayout.loadmoreFinish(mPullLayout.FAIL);
						// 这里是显示有数据和无数据的布局
						if (more_list != null && more_list.size() != 0) {
							mViewHint.setVisibility(View.GONE);
						} else {
							mViewHint.setVisibility(View.VISIBLE);
							return;
						}
					}
				});
	}
	

	@SuppressLint("NewApi")
	/**
	 * 设置选中的条件View
	 * @param arrs
	 */
	private void setTags(final List<SearchRequestBeanSend> list) {
		mLineMain.removeAllViews();
		int c = 0;
		LinearLayout row = new LinearLayout(mContext);
		row.setOrientation(LinearLayout.HORIZONTAL);
		TextView addTag = new TextView(mContext);

		for (int i = 0; i < list.size(); i++) {
			final int x = i;
			addTag = new TextView(mContext);
			addTag.setText("+");
			addTag.setGravity(Gravity.CENTER);
			addTag.setTextColor(Color.BLACK);
			LayoutParams params = new LayoutParams(VeDate.dip2px(mContext, 40),
					VeDate.dip2px(mContext, 30));
			params.setMargins(0, 0, 0, VeDate.dip2px(mContext, 15));
			addTag.setLayoutParams(params);

			String str = list.get(i).getName();
			TextView tag = new TextView(mContext);
			tag.setText(str);
			tag.setGravity(Gravity.CENTER);
			tag.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					list.remove(x);
					page = 1;
					isRefreshed = true;
					searchRequestData();

				}
			});
			// tag.setWidth();
			// tag.setHeight();
			tag.setTextColor(Color.BLACK);
			LayoutParams layoutParams = new LayoutParams(VeDate.dip2px(
					mContext, 70), VeDate.dip2px(mContext, 30));
			tag.setLayoutParams(layoutParams);
			tag.setBackground(ContextCompat.getDrawable(mContext,
					R.drawable.kuangclose));
			c++;

			if (i == 0) {
				TextView first = new TextView(mContext);
				first.setText("筛选条件");
				LayoutParams layoutParams2 = new LayoutParams(VeDate.dip2px(
						mContext, 70), VeDate.dip2px(mContext, 30));
				layoutParams2.setMargins(VeDate.dip2px(mContext, 10), 0,
						VeDate.dip2px(mContext, 10),
						VeDate.dip2px(mContext, 10));
				first.setLayoutParams(layoutParams2);
				first.setGravity(Gravity.CENTER);
				first.setTextColor(Color.BLACK);
				row.addView(first);
				row.addView(tag);
				if (list.size() == 1) {
					mLineMain.addView(row);
				}
				c++;
			} else {
				if (list.size() < 3) {
					row.addView(addTag);
					row.addView(tag);
					if (i == list.size() - 1) {
						mLineMain.addView(row);
					}
				} else {

					if (c == 4) {
						c = 1;
					}
					if (c == 1) {
						mLineMain.addView(row);
						row = new LinearLayout(mContext);
						row.setOrientation(LinearLayout.HORIZONTAL);
						View blankView = new View(mContext);
						blankView.setLayoutParams(new LayoutParams(VeDate
								.dip2px(mContext, 20), 10));
						row.addView(blankView);
						row.addView(tag);

					}
					if (c == 2 || c == 3) {
						row.addView(addTag);
						row.addView(tag);
					}
					if (i == list.size() - 1) {
						mLineMain.addView(row);
					}
				}

			}

		}
	}

}
